package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 亮健好药
 * @author tngou@tngou.net
 *
 */
public class COM_360lj extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_360lj.class);
	
	private String url="http://www.360lj.com/product/{}.html";


	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("请检查网址是否有误")>0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("/");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements procrumbs = doc.getElementsByClass("jk_de_nav");
		if(procrumbs.first()!=null)
		{
			Elements as = procrumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size()-1; i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		//图片
		Elements items = doc.getElementsByClass("jk_de_cont_img_sm");
		if(items.first()!=null)
		{
			Elements imgs = items.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("data-img");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//价格
		Elements price = doc.getElementsByClass("jk_price");
		if(price.first()!=null)
		{			
			String value = price.first().text();		
			json.put("price", getPrice(value));		
		}	
		//类型
		Elements jk_de_ifo = doc.getElementsByClass("jk_de_ifo");
		if(jk_de_ifo.first()!=null)
		{
			Elements h2 = jk_de_ifo.first().getElementsByTag("h2");
			
			if(h2.first()!=null) {
				String s=h2.first().attr("class");
				if(StringUtil.isNotEmpty(s))
				{
					if(s.toUpperCase().indexOf("RX")>=0)
					{
						json.put("ptype", Ptype.RX处方药.ordinal());
					}if(s.toUpperCase().indexOf("OTC")>=0)
					{
						if(s.toUpperCase().indexOf("RED_OTC")>=0)
						{
							json.put("ptype", Ptype.OTC非处方甲类.ordinal());
						
						}else
						{
							json.put("ptype", Ptype.OTC非处方乙类.ordinal());
						}
					
					}
				}
			}
		}
		//说明书
		Elements instruction = doc.getElementsByClass("ifo_card_ins");
		if(instruction.first()!=null)
		{
			JSONObject instructions = instructions(instruction.first());
			
			json.put("instructions",instructions );
			
		}
		return json;
	}
	private JSONObject instructions(Element element) {
		JSONObject json = new JSONObject(true);
		
		String[] sts = StringUtil.split(element.text(), "【");
		for (String e : sts) {	
			String[] es = StringUtil.split(e, "】");	
			if(es.length==2) {
				String key=StringUtil.cleanKey(es[0]);
				String value=StringUtil.RemoveEmpty(es[1]);
				if("药品名称".equals(key)) {
					String t = replaceAll(es[1]);
					String[] ys = StringUtil.split(t, "|");
					for (String s : ys) {
						String[] ss = StringUtil.split(s, "：");
						if(ss.length==2)
						{
							if(!"商品名称".equals(ss[0]))
							json.put(StringUtil.cleanKey(ss[0]), StringUtil.RemoveEmpty(ss[1]));
						}
					}
				}else {
					json.put(key, value);	
				}
				
			}
		}
		
		return name_number(json);
	}
	
}
